﻿using System.Security.Permissions;
using System.IO;
using Word = Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using System.Windows.Forms;
using System.Drawing;
using System.Xml;
using System.Xml.Xsl;
using System.Net;

namespace CMISBrowser4Word
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
            this.webBrowser1.ObjectForScripting = this;
            this.webBrowser1.Url = new System.Uri(System.Configuration.ConfigurationManager.AppSettings["ServerPath"], System.UriKind.Absolute);
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this.Focused.ToString());
        }




        public bool saveTo(string relativePath)
        {

            /*StreamWriter a = File.CreateText("c:\\cmr\\test.docx");
            a.Write(Globals.ThisAddIn.Application.ActiveDocument.Content.WordOpenXML.ToString());
            a.Close();
            */


            return true;
        }

        public bool openDocument(string relativePath)
        {
            return true;
        }

        public bool docHasExtension()
        {
            return true;
        }

        public string saveToAs(string relativePath, string filename)
        {
            return relativePath;
        }

        public string compareDocument(string url)
        {
            return url;
        }

        public string previewDocument(string relativePath, string nodeRef)
        {
            /*Form myPreview = new WebBrowserForm(); ;
            myPreview.Show();*/

            return "a";
        }
        public bool insertDocument(string relativePath, string nodeRef)
        {
            string webdavPath = System.Configuration.ConfigurationManager.AppSettings["ServerPath"] + relativePath;

            /* get content */
            System.Net.WebRequest webRequest = System.Net.WebRequest.Create(webdavPath);
            webRequest.Method = "GET";
            System.Net.WebResponse oResponse = webRequest.GetResponse();

            String MyResult = new StreamReader(oResponse.GetResponseStream(), Encoding.Default).ReadToEnd();

            //HtmlFragment h = new HtmlFragment(MyResult);


            Word.Selection selection = Globals.ThisAddIn.Application.ActiveWindow.Selection;
            object start = selection.Range.Start;
            object end = selection.Range.End;
            Word.Range rng = Globals.ThisAddIn.Application.ActiveDocument.Range(ref start, ref end);

            DataObject myClipBoardData = new DataObject();

            //Create Variables to hold different data formats
            String myString = string.Empty;
            Bitmap myBitmap = null;
            IDataObject ido = Clipboard.GetDataObject();

            //Get the data from the clipboard
            if (ido.GetDataPresent(DataFormats.Bitmap) || ido.GetDataPresent(DataFormats.Text))
            {
                //(string) ido.GetData(DataFormats.Text);
                myString = (String)ido.GetData(DataFormats.Text);
                myBitmap = (Bitmap)ido.GetData(DataFormats.Bitmap);
                //Set the data to my dataobject
                myClipBoardData.SetData(DataFormats.Text, true, myString);
                myClipBoardData.SetData(DataFormats.Bitmap, true, myBitmap);
            }


            HtmlFragment.CopyToClipboard(MyResult);
            HtmlFragment MyTest = HtmlFragment.FromClipboard();
            rng.Paste();

            Clipboard.SetDataObject(myClipBoardData, true);

            object direction = Word.WdCollapseDirection.wdCollapseEnd;
            rng.Collapse(ref direction);
            rng.Select();
            Globals.ThisAddIn.Application.Activate();

            //rng.InsertAfter(MyResult);
            //File.OpenRead(relativePath);
            return false;
        }
    }
}
